var functionJs = {
    data : {
        pageData : {
            total    : 0,
            pageNum  : 1,
            pageSize : 8
        }
    },
    start : function () {
        this.onLoad();
        this.bindEvent();
    },
    onLoad : function () {
        this.loadTableData();
    },
    bindEvent : function () {
        var _this = this;
        $("#function_search_btn").click(function () {
            _this.data.pageData.total   = 0;
            _this.data.pageData.pageNum = 1;
            _this.loadTableData();
        });
        $("#searchStr").keyup(function (e) {
            if(e.keyCode == 13){
                _this.data.pageData.total   = 0;
                _this.data.pageData.pageNum = 1;
                _this.loadTableData();
            }
        });
        $("#reset_search_btn").click(function () {
            $("#searchStr").val("");
        });
        $("#add_function_btn").click(function () {
            _this.addFunction();
        });
        $("#add_functionName").blur(function () {
            _this.validateFunctionName("add_functionName");
        });
        $("#update_functionName").blur(function () {
            _this.validateFunctionName("update_functionName", "update_functionId");
        });
    },
    loadTableData : function () {
        var _this = this;
        sinoUtil.sendHttp(sinoUtil.httpPrefix + '/api/function/getAllFunctionsByParamLimit', 'POST', {
            searchStr : $.trim($('#searchStr').val()),
            pageNum   : _this.data.pageData.pageNum,
            pageSize  : _this.data.pageData.pageSize
        }, function (res) {
            if (res.status == 0){
                layui.use('table', function () {
                    var table = layui.table;
                    table.render({
                            elem    : '#function_table',
                            height  : 352,
                            url     : '',
                            data    : res.data.list,
                            page    : false,
                            cols    : [[
                                {
                                    field    : 'functionName',
                                    title    : '功能名称',
                                    width    : '30%',
                                    minWidth : 100,
                                    align    : 'center'
                                },
                                {
                                    field    : 'functionRightCode',
                                    title    : '功能编码',
                                    width    : '30%',
                                    minWidth : 150,
                                    align    : 'center'
                                },
                                {
                                    field    : 'functionType',
                                    title    : '功能类型',
                                    width    : '10%',
                                    minWidth : 150,
                                    align    : 'center'
                                },
                                {
                                    field    : 'functionState',
                                    title    : '功能状态',
                                    width    : '10%',
                                    minWidth : 150,
                                    align    : 'center'
                                },
                                {
                                    title    : '操作',
                                    fixed    : 'right',
                                    width    : '20%',
                                    align    : 'center',
                                    minWidth : 100,
                                    toolbar  : '#function_table_tool_bar'
                                }
                            ]],
                            done    : function(res, page, count) {
                                $("[data-field='functionType']").children().each(function () {
                                    if ($(this).text() == '0') {
                                        $(this).text("菜单");
                                    } else {
                                        $(this).text("其他");
                                    }
                                });
                                $("[data-field='functionState']").children().each(function () {
                                    if ($(this).text() == '0') {
                                        $(this).text("启用");
                                    } else {
                                        $(this).text("禁用");
                                    }
                                });
                            },
                            text    : {
                                none : '暂无相关数据'
                            }
                    });
                    table.on('tool(function_filter)', function (obj) {
                        var layEvent = obj.event;
                        if (layEvent === 'updateFunction'){
                            _this.updateFunction(obj.data);
                        }
                        else if (layEvent === 'deleteFunction'){
                            _this.deleteFunction(obj.data);
                        }
                    });
                })
                _this.data.pageData.total = res.data.total;
                _this.loadPageContent();
            } else if (res.status == 11 || res.status == 10){
                window.parent.location.href = '/system/login';
            } else {
                _this.data.pageData.total   = 0;
                _this.data.pageData.pageNum = 1;
                layer.msg(res.msg);
            }
        }, function (error) {
            _this.data.pageData.total   = 0;
            _this.data.pageData.pageNum = 1;
            layer.msg('哪里出错了');
        }, false)
    },
    loadPageContent : function () {
        var _this = this;
        layui.use('laypage', function () {
            var laypage = layui.laypage;
            laypage.render({
                elem  : 'function_table_page_content',
                count : _this.data.pageData.total,
                curr  : _this.data.pageData.pageNum,
                limit : _this.data.pageData.pageSize,
                jump  : function (obj, first) {
                    if(!first){
                        _this.data.pageData.pageNum = obj.curr;
                        _this.data.pageData.pageSize = obj.limit;
                        _this.loadTableData();
                    }
                }
            })
        })
    },
    addFunction : function () {
        var _this = this;
        var index = layer.open({
            type: 1,
            title: "新增功能",
            skin: "layui-layer-lan",
            area: ['800px', '520px'],
            content: $("#addFunctionDialog"),
            btn: ['提交', '取消'],
            btnAlign: 'c',
            skin: 'demo-class',
            success: function (layero, index) {

            },
            end: function () {
                $("#add_functionName").val("").removeClass("error").next("p").remove();
                $("#add_functionRight").val("").removeClass("error").next("p").remove();
                $("#add_functionState").val("0").removeClass("error").next("p").remove();
                $("#add_functionType").val("0").removeClass("error").next("p").remove();
            },
            btn1: function(index, layero){
                var loadingIndex = sinoUtil.loading();
                var param = {
                    functionName       : $("#add_functionName").val(),
                    functionRightCode  : $("#add_functionRight").val(),
                    functionState      : $("#add_functionState").val(),
                    functionType       : $("#add_functionType").val()
                }
                if(!sinoUtil.validateAndChangeClass("#add_functionRight", sinoUtil.validate(param.functionRightCode, 'require'), "功能编码不能为空!")){
                    layer.close(loadingIndex);
                    return false;
                }
                if(!sinoUtil.validateAndChangeClass("#add_functionRight", sinoUtil.validate(param.functionRightCode, 'specialCharacter'), "功能编码不能包含特殊字符!")){
                    layer.close(loadingIndex);
                    return false;
                }
                if(!sinoUtil.validateAndChangeClass("#add_functionState", sinoUtil.validate(param.functionState, 'require'), "请选择功能状态!")){
                    layer.close(loadingIndex);
                    return false;
                }
                if(!sinoUtil.validateAndChangeClass("#add_functionType", sinoUtil.validate(param.functionType, 'require'), "请选择功能类型!")){
                    layer.close(loadingIndex);
                    return false;
                }
                sinoUtil.sendHttp(sinoUtil.httpPrefix + "/api/function/addFunction", "POST", param, function(res){
                    if(res.status == 0){
                        layer.close(index);
                        _this.loadTableData();
                        sinoUtil.stopLoadingAndShowResult(loadingIndex, 1, "新增成功");
                    }else if(res.status == 10 || res.status == 11){
                        window.parent.location.href = "/system/login";
                    }else{
                        sinoUtil.stopLoadingAndShowResult(loadingIndex, 2, res.msg);
                    }
                }, function(error){
                    sinoUtil.stopLoadingAndShowResult(loadingIndex, 2, "发生未知错误，请刷新重试");
                }, false);
            }
        });
    },
    updateFunction : function (row) {
        var _this = this;
        var index = layer.open({
            type: 1,
            title: "编辑功能",
            skin: "layui-layer-lan",
            area: ['800px', '520px'],
            content: $("#updateFunctionDialog"),
            btn: ['提交', '取消'],
            btnAlign: 'c',
            skin: 'demo-class',
            success: function (layero, index) {
                $("#update_functionId").val(row.functionId);
                $("#update_functionName").val(row.functionName);
                $("#update_functionRight").val(row.functionRightCode);
                $("#update_functionState").val(row.functionState);
                $("#update_functionType").val(row.functionType);
                layui.use('form', function(){
                    layui.form.render();
                });
            },
            end: function () {
                $("#update_functionId").val("").removeClass("error").next("p").remove();
                $("#update_functionName").val("").removeClass("error").next("p").remove();
                $("#update_functionRight").val("").removeClass("error").next("p").remove();
                $("#update_functionState").val("").removeClass("error").next("p").remove();
                $("#update_functionType").val("").removeClass("error").next("p").remove();
            },
            btn1: function(index, layero){
                var loadingIndex = sinoUtil.loading();
                var param = {
                    functionId         : $("#update_functionId").val(),
                    functionName       : $("#update_functionName").val(),
                    functionRightCode  : $("#update_functionRight").val(),
                    functionState      : $("#update_functionState").val(),
                    functionType       : $("#update_functionType").val()
                }
                if(!sinoUtil.validateAndChangeClass("#update_functionRight", sinoUtil.validate(param.functionRightCode, 'require'), "功能编码不能为空!")){
                    layer.close(loadingIndex);
                    return false;
                }
                if(!sinoUtil.validateAndChangeClass("#update_functionRight", sinoUtil.validate(param.functionRightCode, 'specialCharacter'), "功能编码不能包含特殊字符!")){
                    layer.close(loadingIndex);
                    return false;
                }
                if(!sinoUtil.validateAndChangeClass("#update_functionState", sinoUtil.validate(param.functionState, 'require'), "请选择功能状态!")){
                    layer.close(loadingIndex);
                    return false;
                }
                if(!sinoUtil.validateAndChangeClass("#update_functionType", sinoUtil.validate(param.functionType, 'require'), "请选择功能类型!")){
                    layer.close(loadingIndex);
                    return false;
                }
                sinoUtil.sendHttp(sinoUtil.httpPrefix + "/api/function/updateFunction", "POST", param, function(res){
                    if(res.status == 0){
                        layer.close(index);
                        _this.loadTableData();
                        sinoUtil.stopLoadingAndShowResult(loadingIndex, 1, "修改成功");
                    }else if(res.status == 10 || res.status == 11){
                        window.parent.location.href = "/system/login";
                    }else{
                        sinoUtil.stopLoadingAndShowResult(loadingIndex, 2, res.msg);
                    }
                }, function(error){
                    sinoUtil.stopLoadingAndShowResult(loadingIndex, 2, "发生未知错误，请刷新重试");
                }, false);
            }
        });
    },
    deleteFunction : function (row) {
        var _this = this;
        layer.confirm('数据删除后将不可恢复,确定要删除吗?', function(index){
            layer.close(index);
            var loadingIndex = sinoUtil.loading();
            sinoUtil.sendHttp(sinoUtil.httpPrefix + "/api/function/deleteFunction", "POST", {functionId : row.functionId}, function(res){
                if(res.status == 0){
                    _this.loadTableData();
                    sinoUtil.stopLoadingAndShowResult(loadingIndex, 1, "删除成功");
                }else if(res.status == 10 || res.status == 11){
                    window.parent.location.href = "/system/login";
                }else{
                    sinoUtil.stopLoadingAndShowResult(loadingIndex, 2, res.msg);
                }
            }, function(error){
                sinoUtil.stopLoadingAndShowResult(loadingIndex, 2, "发生未知错误，请刷新重试");
            }, false);
        });
    },
    validateFunctionName : function (target, idTarget) {
        if(!sinoUtil.validateAndChangeClass("#" + target, sinoUtil.validate($("#" + target).val(), 'require'), "功能名称不能为空!")){
            return false;
        }
        if(!sinoUtil.validateAndChangeClass("#" + target, sinoUtil.validate($("#" + target).val(), 'specialCharacter'), "功能名称不能包含特殊字符!")){
            return false;
        }
        if(idTarget){
            sinoUtil.sendHttp(sinoUtil.httpPrefix + "/api/function/validateFunctionNameExSelf", "POST", {functionName : $("#" + target).val(), functionId : $("#" + idTarget).val()}, function(res){
                if(res.status == 0){
                    sinoUtil.validateAndChangeClass("#" + target, true);
                }else if(res.status == 10 || res.status == 11){
                    window.location.href = "/system/login";
                }else{
                    sinoUtil.validateAndChangeClass("#" + target, false, "功能名称不能重复");
                }
            }, function(error){
                layer.msg("发生未知错误，请刷新重试");
            }, false);
        }else{
            sinoUtil.sendHttp(sinoUtil.httpPrefix + "/api/function/validateFunctionName", "POST", {functionName : $("#" + target).val()}, function(res){
                if(res.status == 0){
                    sinoUtil.validateAndChangeClass("#" + target, true);
                }else if(res.status == 10 || res.status == 11){
                    window.location.href = "/system/login";
                }else{
                    sinoUtil.validateAndChangeClass("#" + target, false, "功能名称不能重复");
                }
            }, function(error){
                layer.msg("发生未知错误，请刷新重试");
            }, false);
        }
    }

}
$(function () {
    functionJs.start();
})